package com.junzhiit.nengji.app.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.app.service.GoodsSkuService;
import com.junzhiit.nengji.app.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.app.web.util.ViewObjectUtil;
import com.junzhiit.nengji.app.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.GoodsSkuItemsDetailVO;
import com.junzhiit.nengji.app.web.vo.GoodsSkuItemsVO;
import com.junzhiit.nengji.app.web.vo.GoodsSkuVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.bo.GoodsSkuBO;
import com.junzhiit.nengji.bo.GoodsSkuItemsBO;
import com.junzhiit.nengji.dao.CartDAO;
import com.junzhiit.nengji.dao.GoodsSkuItemsDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.SkuContentDAO;
import com.junzhiit.nengji.dao.SkuDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.GoodsSkuItems;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.Sku;
import com.junzhiit.nengji.model.SkuContent;
import com.junzhiit.nengji.model.SkuItems;
import com.junzhiit.nengji.model.enums.Status;

@Service
public class GoodsSkuServiceImpl extends BaseServiceImpl implements GoodsSkuService {

	@Autowired
	private GoodsSkuItemsDAO goodsSkuItemsDAO;

	@Autowired
	private SkuContentDAO skuContentDAO;

	@Autowired
	private SkuDAO skuDAO;

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private CartDAO cartDAO;

	@Value("${upload.mall.goods.sku}")
	private String skuPath;

	@Value("${upload.temp}")
	private String uploadPath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;

	@Override
	public ResultVO queryGoodsSku(String sessionId) {
		ResultVO result = new ResultVO();

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		List<GoodsSkuBO> goodsSkuBOs = goodsSkuItemsDAO.queryGoodsSku(shop.getId());

		if (goodsSkuBOs.isEmpty()) {
			return result;
		}

		List<Sku> skus = skuDAO.queryAllSku(shop.getId());

		List<GoodsSkuVO> goodsSkuVOs = new ArrayList<GoodsSkuVO>();
		for (Sku sku : skus) {
			GoodsSkuVO goodsSkuVO = new GoodsSkuVO();
			String id = sku.getId();

			List<SkuItems> skuItemses = new ArrayList<SkuItems>();
			for (GoodsSkuBO goodsSkuBO : goodsSkuBOs) {
				String uid = goodsSkuBO.getUid();
				if (id.equals(uid)) {
					SkuItems skuItems = new SkuItems();
					BeanUtils.copyProperties(goodsSkuBO, skuItems);
					skuItemses.add(skuItems);
				}

			}

			goodsSkuVO.setId(id);
			goodsSkuVO.setName(sku.getName());
			goodsSkuVO.setSkuItemses(skuItemses);

			goodsSkuVOs.add(goodsSkuVO);
		}

		result.setData(goodsSkuVOs);
		return result;
	}

	@Override
	public ResultVO queryGoodsSkuItems(String goodsId) {
		ResultVO result = new ResultVO();
		List<GoodsSkuItems> data = goodsSkuItemsDAO.queryGoodsSkuItems(goodsId);
		result.setData(data);
		return result;
	}

	@Override
	public Map<String, Object> findAllByGoodsId(String goodsId, DataTablesParamVO dtp) {
		List<SkuContent> list = skuContentDAO.findAllByGoodsId(goodsId, dtp.getStart(), dtp.getLength());
		int totalCount = skuContentDAO.getTotalCountByGoodsId(goodsId);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO queryGoodsSkuItemsBO(String goodsId){
		ResultVO result=new ResultVO();
		List<GoodsSkuItemsBO> goodsSkuItemsBOs=goodsSkuItemsDAO.queryGoodsSkuItemsBO(goodsId);
		
		Map<String, GoodsSkuItemsVO> map = new LinkedHashMap<String, GoodsSkuItemsVO>();
		
		for (GoodsSkuItemsBO bo : goodsSkuItemsBOs) {
			GoodsSkuItemsVO rbo = map.get(bo.getSkuId());
	        if (rbo==null) {
	        	rbo = new GoodsSkuItemsVO();
	        	rbo.setName(bo.getName());
	        	rbo.setSkuId(bo.getSkuId());
	        	rbo.setIsOpenImage(bo.getIsOpenImage());
	            List<GoodsSkuItemsDetailVO> childrens = new ArrayList<GoodsSkuItemsDetailVO>();
	            rbo.setChildrens(childrens);
	            map.put(bo.getSkuId(),rbo);
	        }
	        
	        GoodsSkuItemsDetailVO goodsSkuItemsDetailVO=new GoodsSkuItemsDetailVO();
	        goodsSkuItemsDetailVO.setSkuItemsId(bo.getSkuItemsId());
	        goodsSkuItemsDetailVO.setStandard(bo.getStandard());
	        goodsSkuItemsDetailVO.setImage(bo.getImage());
	        goodsSkuItemsDetailVO.setIsOpenImage(bo.getIsOpenImage());
	        rbo.getChildrens().add(goodsSkuItemsDetailVO);
	    }
		Collection<GoodsSkuItemsVO> data = map.values();
		result.setData(data);
		return result;
	}
	
	
	@Override
	public ResultVO querySkuContent(String chainGoodsId) {
		ResultVO result = new ResultVO();
		List<SkuContent> data = skuContentDAO.querySkuContent(chainGoodsId);
		result.setData(data);
		return result;
	}

	@Transactional
	@Override
	public ResultVO addGoodsSku(List<SkuContent> skuContents, List<GoodsSkuItems> goodsSkuItemses, String goodsId) {
		ResultVO result = new ResultVO();
		
		skuContentDAO.deleteSkuContent(goodsId);
		goodsSkuItemsDAO.deleteGoodsSkuItems(goodsId);
		skuContentDAO.batchInsert(skuContents);
		goodsSkuItemsDAO.batchInsert(goodsSkuItemses);
		
		//移动sku图片
		for(GoodsSkuItems goodsSkuItems:goodsSkuItemses){
			if(StringUtils.isNotBlank(goodsSkuItems.getImage())){
				//FileUtil.moveFile(uploadPath + goodsSkuItems.getImage(), skuPath);
				boolean is = AliyunOSSClientUtil.moveFile(backetName,goodsSkuItems.getImage(), skuPath, uploadPath);
				if(is){
					AliyunOSSClientUtil.removeFile(backetName, uploadPath+goodsSkuItems.getImage());
				}
			}
		}
		
		//购物规格失效
		cartDAO.updateStatus(goodsId, Status.INACTIVE.toIntStatus());
		return result;
	}

	@Transactional
	@Override
	public ResultVO updateGoodsSkuItemsImage(String imageUrl){
		ResultVO result = new ResultVO();
		goodsSkuItemsDAO.updateGoodsSkuItemsImage(imageUrl);
		return result;
		
	}
}
